﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace NDF.Framework.ExpressTool
{
    public partial class Bootstrap : Form
    {
        public Bootstrap()
        {
            InitializeComponent();

            this.LoadListBoxData();
        }


        private void LoadListBoxData()
        {
            var types = Assembly.GetExecutingAssembly().GetTypes().Where(t => t.IsSubclassOf(typeof(Form)) && t.FullName != this.GetType().FullName);
            this.listBox1.BeginUpdate();
            foreach (var type in types)
            {
                this.listBox1.Items.Add(type.FullName);
            }
            this.listBox1.EndUpdate();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (this.listBox1.SelectedIndex == ListBox.NoMatches)
            {
                MessageBox.Show(this, "请选择一个窗口", "操作提醒", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            this.OpenWindow(this.listBox1.Text);
        }

        private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            var i = this.listBox1.IndexFromPoint(e.Location);
            if (i != ListBox.NoMatches)
            {
                var text = this.listBox1.Items[i].ToString();
                this.OpenWindow(text);
            }
        }

        protected void OpenWindow(string typeName)
        {
            var assembly = Assembly.GetExecutingAssembly();
            var type = assembly.GetType(typeName);
            var win = Activator.CreateInstance(type) as Form;
            win.Show();
        }
    }
}
